﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Algorithm4Fun
{
    public class RemoveMultipleSpaces
    {
        public int Go(char[] baseStr)
        {
            int r = 0, w = 0;
            char c;
            while (r < baseStr.Length)
            {
                c = baseStr[r];
                baseStr[w++] = c;
                r++;
                if (c == ' ')
                {
                    while (r < baseStr.Length && baseStr[r] == ' ')
                        r++;
                }
            }
            return w;
        }

        public void SendSM(string vAccepter)
        {
            try
            {
                List<string> tmpList = vAccepter.Split(',').Where(o => o != string.Empty).Distinct().ToList();
                
                 
                    string msgList = string.Empty;
                    foreach (string str in tmpList)
                    {
                        if (msgList == string.Empty)
                        {

                            msgList = "张静婷";
                        }

                        else
                        {
                            msgList += ",张静婷";

                        }
                    }
                    vAccepter = msgList;

                    Console.WriteLine("vAccepter:{0}", vAccepter); 
                int counter = 0;
                string msgListToSend = string.Empty;

                var arr = vAccepter.Split(',');

                for (int i = 0; i < arr.Length; i++)
                {
                    counter++;

                    if (msgListToSend == string.Empty)
                    {

                        msgListToSend = arr[i];
                    }

                    else
                    {
                        msgListToSend += "," + arr[i];

                    }

                    if (i < arr.Length - 1)
                    {
                        if (counter == 10)
                        {
                            Console.WriteLine("msgListToSend:{0},counter:{1}", msgListToSend, counter); 
                            msgListToSend = "";
                            counter = 0;
                        }
                    }
                    else
                    {
                        Console.WriteLine("msgListToSend:{0},counter:{1}", msgListToSend, counter); 
                        msgListToSend = "";
                        counter = 0;
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    [TestFixture]
    public class MyTest
    {
        char[] input = null;
        int resultLength = 0;
        RemoveMultipleSpaces rms = new RemoveMultipleSpaces();
        [Test]
        public void Test1()
        {
            input = "abc    def".ToCharArray();
            resultLength = rms.Go(input);

            Assert.AreEqual(7, resultLength);
            Assert.AreEqual("abc def", new string(input, 0, resultLength));
        
        }

        [Test]
        public void Test2()
        {
            input = "abc    def       ghi".ToCharArray();
            resultLength = rms.Go(input);

            Assert.AreEqual(11, resultLength);
            Assert.AreEqual("abc def ghi", new string(input, 0, resultLength));
        }

        [Test]
        public void Test3()
        {
            input = " a b   d e ".ToCharArray();
            resultLength = rms.Go(input);

            Assert.AreEqual(9, resultLength);
            Assert.AreEqual(" a b d e ", new string(input, 0, resultLength));
        }

        [Test]
        public void Test4()
        {
            input = "             ".ToCharArray();
            resultLength = rms.Go(input);

            Assert.AreEqual(1, resultLength);
            Assert.AreEqual(" ", new string(input, 0, resultLength));
        }

        [Test]
        public void TestSendSM()
        {
            rms.SendSM("1,2,3,4,5,6,7,8,9,10,11,E,F,G,H,B,C,D,k,l");
        }

    }  

 
}
